﻿#include "xml/Inc/xmlstreamreader.h"

XmlStreamReader::XmlStreamReader(QObject *parent) : QObject(parent)
{

}
void XmlStreamReader::creatXML(QString fileName)
{
    QFile file(fileName);
    if(file.exists() == true)
    {
        qDebug() << "file exit!" << endl;
        return ;
    }
    else
    {
        bool isOK = file.open(QIODevice::ReadWrite);
        if(isOK == true)
        {
            QDomDocument doc;
            QDomProcessingInstruction ins;
            ins = doc.createProcessingInstruction("xml","version=\'1.0\' encoding=\'utf-8\'");
            doc.appendChild(ins);
            QDomElement root = doc.createElement("basicParams");
            doc.appendChild(root);

            QTextStream stream(&file);
            doc.save(stream,4);
            file.close();
        }
        else
        {
            qDebug() << "file open error !" << endl;
            return ;
        }
    }
}
void XmlStreamReader::appendXML(QString filePath , QStringList list)
{
    QFile file(filePath);

    bool isOK = file.open(QIODevice::ReadWrite);
    if(isOK == true)
    {
        QDomDocument doc;
        isOK =  doc.setContent(&file);
        if(isOK == true)
        {
            file.close();
            QDateTime date = QDateTime::currentDateTime();
            QString time  = date.toString("yyyy-MM-dd");
            QDomElement root = doc.documentElement();
            if(root.hasChildNodes())
            {
                qDebug() << "file has childNodes" << endl;
                return ;
            }
            else
            {
                QDomElement dateElm = doc.createElement("日期");
                QDomAttr dateAttr = doc.createAttribute("date");
                dateAttr.setNodeValue(time);
                dateElm.setAttributeNode(dateAttr);
                root.appendChild(dateElm);

                QDateTime date = QDateTime::currentDateTime();
                QString time  = date.toString("hh-mm-ss");
                QDomElement timeElm = doc.createElement("时间");
                QDomAttr timeAttr = doc.createAttribute("time");
                timeAttr.setNodeValue(time);
                timeElm.setAttributeNode(timeAttr);
                dateElm.appendChild(timeElm);
                writeXML(doc,timeElm,list);
            }
            isOK = file.open(QIODevice::ReadWrite);
            if(isOK == true)
            {
                QTextStream stream(&file);
                doc.save(stream,4);
                file.close();
            }
        }
        else
        {
            qDebug() << "setContent error !" << endl;
            return;
        }
    }
    else
    {
        qDebug() << "open file error !" << endl;
    }
}

void XmlStreamReader::writeXML(QDomDocument &doc,QDomElement &root,QStringList &list)
{

    QDomElement cameraElm = doc.createElement("参数");
    QDomAttr cameraAttr = doc.createAttribute("Params");
    cameraAttr.setNodeValue("camera");
    cameraElm.setAttributeNode(cameraAttr);
    root.appendChild(cameraElm);

    QDomElement armorElm = doc.createElement("参数");
    QDomAttr armorAttr = doc.createAttribute("Params");
    armorAttr.setNodeValue("armor");
    armorElm.setAttributeNode(armorAttr);
    root.appendChild(armorElm);

    QDomElement buffElm = doc.createElement("参数");
    QDomAttr buffAttr = doc.createAttribute("Params");
    buffAttr.setNodeValue("buff");
    buffElm.setAttributeNode(buffAttr);
    root.appendChild(buffElm);


    QDomElement paramsCamera = doc.createElement("相机参数");
    QDomElement width = doc.createElement("宽度");
    QDomElement height = doc.createElement("高度");
    QDomElement videoAOIX = doc.createElement("AOI偏移X");
    QDomElement videoAOIY = doc.createElement("AOI偏移Y");
    QDomElement exposureTime = doc.createElement("曝光时间");

    QDomElement paramsArmor = doc.createElement("装甲板参数");
    QDomElement enemyColor = doc.createElement("敌方颜色");
    QDomElement offsetXArmor = doc.createElement("偏移X");
    QDomElement offsetYArmor = doc.createElement("偏移Y");

    QDomElement paramsBuff = doc.createElement("神符参数");
    QDomElement offsetXBuff = doc.createElement("偏移X");
    QDomElement offsetYBuff = doc.createElement("偏移Y");
    QDomElement vGain = doc.createElement("速度增益");

    QDomText text =doc.createTextNode(list.at(0));
    paramsCamera.appendChild(text);
    text = doc.createTextNode(list.at(1));
    width.appendChild(text);
    text = doc.createTextNode(list.at(2));
    height.appendChild(text);
    text = doc.createTextNode(list.at(3));
    videoAOIX.appendChild(text);
    text = doc.createTextNode(list.at(4));
    videoAOIY.appendChild(text);
    text = doc.createTextNode(list.at(5));
    exposureTime.appendChild(text);

    text = doc.createTextNode(list.at(6));
    paramsArmor.appendChild(text);
    text = doc.createTextNode(list.at(7));
    enemyColor.appendChild(text);
    text = doc.createTextNode(list.at(8));
    offsetXArmor.appendChild(text);
    text = doc.createTextNode(list.at(9));
    offsetYArmor.appendChild(text);

    text = doc.createTextNode(list.at(10));
    paramsBuff.appendChild(text);
    text = doc.createTextNode(list.at(11));
    offsetXBuff.appendChild(text);
    text = doc.createTextNode(list.at(12));
    offsetYBuff.appendChild(text);
    text = doc.createTextNode(list.at(13));
    vGain.appendChild(text);

    cameraElm.appendChild(paramsCamera);
    cameraElm.appendChild(width);
    cameraElm.appendChild(height);
    cameraElm.appendChild(videoAOIX);
    cameraElm.appendChild(videoAOIY);
    cameraElm.appendChild(exposureTime);

    armorElm.appendChild(paramsArmor);
    armorElm.appendChild(enemyColor);
    armorElm.appendChild(offsetXArmor);
    armorElm.appendChild(offsetYArmor);

    buffElm.appendChild(paramsBuff);
    buffElm.appendChild(offsetXBuff);
    buffElm.appendChild(offsetYBuff);
    buffElm.appendChild(vGain);
}

void XmlStreamReader::readXML(QString filePath , QStringList &listCamera ,QStringList &listArmor,QStringList &listBuff)
{
    QFile file(filePath);
    bool isOK = file.open(QIODevice::ReadWrite);
    if(isOK == true)
    {
        QDomDocument doc;
        isOK =  doc.setContent(&file);
        file.close();
        if(isOK == true)
        {
            QDomElement root = doc.documentElement();
            if(root.hasChildNodes())
            {
                QDomElement lastElm = root.lastChildElement();
                QDomNodeList nodelist = lastElm.childNodes();
                QDomNodeList sublist = nodelist.at(0).toElement().childNodes();
                for(int i=0;i<sublist.size();i++)
                {
                    QDomNodeList sublist2 = sublist.at(i).toElement().childNodes();
                    for(int j=0;j<sublist2.size();j++)
                    {
                        if(i == 0)
                        {
                            listCamera << sublist2.at(j).toElement().text();
                            qDebug() << sublist2.at(j).toElement().text()<<endl;
                        }
                        else if(i ==1)
                        {
                            listArmor << sublist2.at(j).toElement().text();
                            qDebug() << sublist2.at(j).toElement().text()<<endl;
                        }
                        else
                        {
                            listBuff << sublist2.at(j).toElement().text();
                            qDebug() << sublist2.at(j).toElement().text()<<endl;
                        }
                    }
                }
            }
            else
            {
                qDebug() <<"no child node" <<endl;
                return ;
            }
        }
        else
        {
            qDebug() << "setContent error!" << endl;
            return ;
        }
    }
    else
    {
        qDebug() << "readwrite error !" << endl;
        return ;
    }
}


